package com.example.day09;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.List;

public class Recy_RecyAdapter extends RecyclerView.Adapter {
    List<Bean.DataBean.DatasBean>beanList;
    List<BannerBean.DataBean>bannerlist;
    Context context;


    public Recy_RecyAdapter(List<Bean.DataBean.DatasBean> beanList, List<BannerBean.DataBean> bannerlist, Context context) {
        this.beanList = beanList;
        this.bannerlist = bannerlist;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 1) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.activity_banner, parent, false);
            return new ViewHoler1(inflate);
        } else {
            View inflate = LayoutInflater.from(context).inflate(R.layout.activity_recy, parent, false);
            return new ViewHoler2(inflate);
        }
    }
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        if(viewType==1){
            ViewHoler1 viewHoler1= (ViewHoler1) holder;
            viewHoler1.banner.setImages(bannerlist).setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    BannerBean.DataBean a= (BannerBean.DataBean) path;
                    Glide.with(context).load(a.getImagePath()).into(imageView);
                }
            }).start();
        }
        if (viewType==2){
            int _position=position;
             if(bannerlist.size()>0){
                 _position-=1;
             }
             ViewHoler2 viewHoler2= (ViewHoler2) holder;
             viewHoler2.t2.setText(beanList.get(_position).getUserId()+"");
             viewHoler2.t3.setText(beanList.get(_position).getChapterName()+"");
             Glide.with(context).load(beanList.get(_position).getEnvelopePic()).into(viewHoler2.img);
        }
    }

    @Override
    public int getItemCount() {
        if(bannerlist.size()>0){
            return beanList.size()+1;
        }else {
            return beanList.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position==0){
            return 1;
        }else {
            return 2;
        }
    }
    public class ViewHoler1 extends RecyclerView.ViewHolder{
      public   Banner banner;
        public ViewHoler1(@NonNull View itemView) {
            super(itemView);
            banner=itemView.findViewById(R.id.ban);
        }
    }
    public class ViewHoler2 extends RecyclerView.ViewHolder{
            ImageView img;
            TextView t1;
            TextView t2;
            TextView t3;

        public ViewHoler2(@NonNull View itemView) {
            super(itemView);
            img=itemView.findViewById(R.id.img);
            t1=itemView.findViewById(R.id.t1);
            t2=itemView.findViewById(R.id.t2);
            t3=itemView.findViewById(R.id.t3);
        }
    }
}
